# 25. 最优结果的a数组数量
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let a;
let b;
let maxMatch = 0;
let op = 0;
function swap(array, i,j) {
[arr[i], arr[j]] = [arr[j], arr[i]];
}
function findMaxMatch(a, b, index) {
if (index === a.length) {
let match = 0;
for(let i=0; i<a.length; i++) {
if (a[i] > b[i]) {
match++;
}
}
maxMatch = Math.max(maxMatch, match);
return;
}
for(let i=index; i<a.length; i++) {
swap(a, index, i);
findMaxMatch(a, b, index+1);
swap(a, index, i);//回溯
}
}
function permute(a, index, b) {
if (index === a.length) {
let match = 0;
for(let i=0;i<a.length; i++) {
if (a[i] > b[i]) {
match++;
}
}
if (match === maxMatch) {
op++;
}
return;
}
for(let i=index; i<a.length; i++) {
swap(a, index, i);
permute(a, index+1, b);
swap(a, index, i);
}
}
rl.on('line', (line) => {
const inputs = line.split(' ').map(Number);
if (!a) {
a = inputs;
} else {
b = inputs;
}
});
rl.on('close', () => {
b.sort((x,y)=>x-y);
findMaxMatch(a, b, 0);
permute(a, 0, b);
console.log(op);
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65